package com.jy.admin.rest.sms;

import com.jsoniter.output.JsonStream;
import com.jy.admin.annotations.AdminSignIgnore;
import com.jy.admin.auth.anno.VisitorAccessible;
import com.jy.admin.db.dao.SmsSendChannelDao;
import com.jy.admin.db.dao.SmsSendLogDao;
import com.jy.admin.db.dao.SmsSendTaskDao;
import com.jy.admin.db.dao.SmsTemplateDao;
import com.jy.admin.db.entity.SmsSendChannelEntity;
import com.jy.admin.db.entity.SmsSendLogEntity;
import com.jy.admin.db.entity.SmsSendTaskEntity;
import com.jy.admin.db.entity.SmsTemplateEntity;
import com.jy.admin.enums.SmsLogStatusEnum;
import com.jy.bo.common.conf.api.ApiResponse;
import com.robert.vesta.util.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/12/11
 * Time: 11:44 上午
 */

@RestController
@RequestMapping("/sms")
public class SmsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private SmsSendChannelDao smsSendChannelDao;

    @Autowired
    private SmsTemplateDao smsTemplateDao;

    @Autowired
    private SmsSendTaskDao smsSendTaskDao;

    @RequestMapping("/task/add")
    public ApiResponse add(@RequestBody SmsUpdateRequest request) {

        SmsSendTaskEntity taskEntity = new SmsSendTaskEntity()
                .setTaskNo(IdUtils.genId() + "")
                .setName(request.getName())
                .setProvider(request.getProvider())
                .setStatus(99)
                .setChannleCode(request.getChannelCode())
                .setTemplateNo(request.getTemplateCode())
                .setDataNo(request.getDataNo())
                .setJumpUrl(request.getJumpUrl())
                .setCreateDt(new Date());

        Map<String, String> params = new HashMap<>();
        params.put("##appName##", request.getAppName());
        params.put("##maxLoan##", request.getMaxLoan());
        params.put("##auditTime##", request.getAuditMin());

        taskEntity.setParams(JsonStream.serialize(params));
        smsSendTaskDao.insert(taskEntity);

        return new ApiResponse();
    }

    @RequestMapping("/channel/list")
    public ApiResponse<ChannelListResponse> channleList() {
        List<SmsSendChannelEntity> allChannels = smsSendChannelDao.getAllChannels();
        return new ApiResponse(new ChannelListResponse(allChannels));
    }

    @RequestMapping("/template/list")
    public ApiResponse templateList() {
        List<SmsTemplateEntity> allTemplates = smsTemplateDao.getAllTemplates();
        return new ApiResponse(new TemplateListResponse(allTemplates));
    }

    @RequestMapping("/task/list")
    public ApiResponse list(HttpServletRequest request) {

        String jumpUrl = "https://play.kyyy.in";
        return new ApiResponse();
    }

    @RequestMapping("/task/update")
    public ApiResponse update(HttpServletRequest request) {

        String jumpUrl = "https://play.kyyy.in";
        return new ApiResponse();
    }
}
